//import {getxm,getxm1} from "api.js";
import { index,getUserZzjgList } from "./utils/zzjg.js";
import { unixtimetostr } from "./utils/tools.js";

axios.defaults.headers.post['Content-Type'] = 'application/json';

// 注册
Vue.component('xtest', {
    template: '<h1>自定义组件!</h1>'
});

var vm = new Vue({
    el: '#app',
    mounted: function () {
        // 访问组件的 DOM 元素
        //console.log(this.$el)
        let that = this;
        // 使用 $nextTick 方法访问父组件的数据
        this.$nextTick(() => {
            console.log('使用 $nextTick 方法访问父组件的数据', this.form)
            this.init_data();
        })
        // 监听数据变化
        this.$watch('data', () => {
            console.log('data changed')
        })
        // 在组件销毁时取消异步操作
        this.$once('hook:beforeDestroy', () => {
            this.cancelAsyncTask()
        })
    },
    data: function () {
        return {
            form: {
                q_work: null,
                zzjg_ids:null,
                selectYear:null,
                yszt_id:null,
                hkzt_id:null,
            },
            tableData: {
                pageSize: 10, pageNumber: 1, total: 10, totalPage: 1, rows: [{}]
            },
            list: {
                zzjgList:[{value:9,label:'所有'},{value:'thisyear',label:'今年'},{value:'lastyear',label:'上一年'}],
                yearList:[{value:9,label:'所有'},{value:'thisyear',label:'今年'},{value:'lastyear',label:'上一年'}],
                ysztList:[{value:9,label:'所有'},{value:1,label:'已验收'},{value:0,label:'未验收'}],
                hkztList:[{value:9,label:'所有'},{value:1,label:'已回款'},{value:0,label:'未回款'}],
            },
        }
    },

    methods: {
        onSubmit() {
            let that = this;
            let form = that.form;
            //时间转换成Unix时间
            //form.ht_time = Date.parse(new Date(form.ht_time)) / 1000;
            console.log("提交表单：", form);

                    that.aj1(form).then($f => {
                        console.log('保存文件：', $f);
                        this.$message.error('表单填写有误，请检查后再提交');
                        that.aj2($f);
                    }).catch(err => {
                        // 处理请求错误
                        this.$message.error('表单填写有误，请检查后再提交');
                        console.error(err); // 打印错误信息
                    });

        },
        onSearch1() {
            let that = this;
            let params = {
                pageNumber: that.tableData.pageNumber,
                pageSize: that.tableData.pageSize,
                searchType: null,
                searchKey: that.form.q_word,
                timestr: that.form.selectYear,
                yszt:that.form.yszt_id,
                hkzt:that.form.hkzt_id,
                zzjg_ids:that.form.zzjg_ids,
            };
            this.search1(params);
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        backurl() {
            window.history.back();  //返回上一页
        },
        tableRowClassName({ row, rowIndex }) {
            if (rowIndex === 1) {
                return 'warning-row';
            } else if (rowIndex === 3) {
                return 'success-row';
            }
            return '';
        },
        tableHandleClick(row) {
            console.log(row);
        },
        ClickEdit(row){
            console.log('点击：',row.id);
        },
        //修改分页大小
        tableHandleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            let that = this;
            let params = {
                pageNumber: that.tableData.pageNumber,
                pageSize: val,
                searchType: null,
                searchKey: that.form.q_word,
                timestr: that.form.selectYear,
                yszt:that.that.form.yszt_id,
                hkzt:that.that.form.hkzt_id,
                zzjg_ids:that.form.zzjg_ids,
            };
            this.search1(params);
        },
        //换页
        tableHandleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            let that = this;
            let params = {
                pageNumber: val,
                pageSize: that.tableData.pageSize,
                searchType: null,
                searchKey: that.form.q_word,
                timestr: that.form.selectYear,
                yszt:that.form.yszt_id,
                hkzt:that.form.hkzt_id,
                zzjg_ids:that.form.zzjg_ids,
            };
            that.search1(params);
        },

    },
});

vm.unixtimetostr = unixtimetostr;

vm.init_data = function(){
    let that = this;

    that.search1();

    const searchParams = new URLSearchParams(window.location.search);
    let id = searchParams.get('id');
    console.log("url params: id,", searchParams.get('id'));   // 输出: 123
}

/**
 * @param {
 *      pageNumber: that.tableData.pageNumber,
 *      pageSize: that.tableData.pageSize,
 *      searchType: null,
 *      q_word: that.form.q_word} params 
 * @returns 
 */
vm.search1 = function (params) {
    let that = vm;
    return new Promise((resolve, reject) => {
        index(params).then((res)=>{
            console.log("zzjg.index:",res);
            that.tableData = res.data;
        }).catch((res)=>{
            console.log("zzjg.index ERR",res);
        });
    });
}

